import {
  IsString,
  IsEmail,
  IsBoolean,
  IsOptional,
  MinLength,
  MaxLength,
  IsMobilePhone,
  Validate,
  IsNotEmpty,
} from 'class-validator';
import { applyDecorators } from '@nestjs/common';
import {
  StartWithConstraint,
  // StartsWith,
  StartWithConstraintSync,
  // StartsWithSync,
} from '../validators/user-validator';
import { ApiProperty, ApiPropertyOptional, PartialType } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import { i18nValidationMessage } from 'nestjs-i18n';
import {
  PartialType as NestMappedPartialType,
  OmitType,
} from '@nestjs/mapped-types';
import {
  IdValidator,
  SortValidator,
  StatusValidator,
} from '../decorators/dto.decorator';

export class CreateUserDto {
  // 配合@ApiResponse({ status: 200, description: '创建成功', type: CreateUserDto })的说明添加
  @ApiProperty({ description: '用户姓名', example: 'user_name1' })
  @UsernameValidator()
  username: string;

  @ApiProperty({ description: '用户密码', example: 'password' })
  @PasswordValidator()
  password: string;

  @ApiProperty({ description: '用户手机号', example: '17633636955' })
  @ApiPropertyOptional() //swagger非必填装饰器，可选的可填可不填，可有可无
  @MobileValidator()
  mobile: string;

  @ApiProperty({ description: '用户邮箱', example: '17633636955@qq.com' })
  @EmailValidator()
  email: string;

  @ApiProperty({ description: '用户状态', example: 1 })
  @StatusValidator()
  status: number;

  @ApiProperty({ description: '是否超级管理员', example: true })
  @IsSuperValidator()
  is_super: boolean;

  @ApiProperty({ description: '用户列表排序', example: 1 })
  @SortValidator()
  sort: number;
}

// PartialType 把每个属性变为可选的
export class UpdateUserDto extends PartialType(
  //PartialType 再将文档设为可选
  // OmitType 忽略字段 password
  OmitType(NestMappedPartialType(CreateUserDto), ['password', 'username']), //NestMappedPartialType 先将校验规则设置可选
) {
  @IdValidator()
  id: number;

  // 重写password属性
  @ApiProperty({ description: '用户密码', example: 'password' })
  @IsOptional()
  password: string;

  // 重写username
  @ApiProperty({ description: '用户姓名', example: 'user_name1' })
  @IsOptional()
  username: string;
}

// 装饰器组合
function UsernameValidator() {
  return applyDecorators(
    IsString(),
    Validate(StartWithConstraint), //第二种使用方式
    Validate(StartWithConstraintSync, [], {
      message: i18nValidationMessage('validation.usernameIsNotUnique'),
    }),
    // StartsWith('user_', { message: '用户名必须以user_开头' }),
  );
}

function PasswordValidator() {
  return applyDecorators(
    IsNotEmpty({
      message: i18nValidationMessage('validation.isNotEmpty', {
        field: 'password',
      }),
    }),
    // message 设置错误信息，这里使用i18n方法去替换
    MaxLength(16, {
      // i18nValidationMessage 参数1 key，参数2变量对象
      message: i18nValidationMessage('validation.maxLength', {
        field: 'password',
        length: 16,
      }),
    }),
    MinLength(6, {
      message: i18nValidationMessage('validation.minLength', {
        field: 'password',
        length: 6,
      }),
    }),
    IsString(),
  );
}

function MobileValidator() {
  return applyDecorators(IsMobilePhone());
}

function EmailValidator() {
  return applyDecorators(IsEmail());
}

function IsSuperValidator() {
  return applyDecorators(
    IsBoolean(),
    Type(() => Boolean),
  );
}
